package rjeutsky.ui;

import java.util.List;

import rjeutsky.ui.models.ItemPrinter;

public class ConsoleSelectorRequest <T> extends ConsoleRequest<T> {

	private List<T> options;
	private ItemPrinter<T> printer;
	
	public ItemPrinter<T> getPrinter() {
		return printer;
	}

	public void setPrinter(ItemPrinter<T> printer) {
		this.printer = printer;
	}
	
	public List<T> getOptions() {
		return options;
	}

	public void setOptions(List<T> options) {
		this.options = options;
	}

	@Override
	protected void queryConsole() {
		int index = 0;
		IntegerConsoleRequest request = new IntegerConsoleRequest();
		request.setRequestText("enter variant number");
		while (!(index > 0 && index <= options.size())) {
			request.Run();
			index = request.value;
		}
		
		value = options.get(index-1);
	}
	
	@Override
	protected void showRequest() {
		super.showRequest();
		int i = 0;
		for (T option : options) {
			System.out.println(String.format("%1$d - %2$s", ++i, itemToString(option)));
		}
	}
	
	private String itemToString (T item) {
		if (printer == null) {
			return item.toString();
		}
		
		return printer.print(item);
	}

}
